<!--  Ofri Keidar 302933981 group 05 -->

<%@page import="java.util.Calendar"%>
<%@page import="java.util.Locale"%>
<%@page import="java.text.DateFormatSymbols"%>
<%@page import="model.database.DBException"%>
<%@page import="controller.ManagerController"%>
<%@page import="view.WebCommons"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<jsp:useBean id="dateData" class="view.useBeans.DateData" scope="page"/>
<jsp:setProperty name="dateData" property="*"/>

<html>
<head>
<link rel="stylesheet" href="../../style/myTemplate/style.css"
	type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sales Report</title>
</head>
<body>

	<span class="customHeader2">Sales Report</span>

	<div id="page">
		<div class="post">
		
			<div id="sidebar">
				<ul>
					<li>
						<h2>Please enter date (yyyy-mm-dd) to get the report for</h2>
					</li>
				</ul>
			</div>
			
			<div id="content">
<!-- 			get time period for report -->
				<form METHOD=POST ACTION="GetSalesReport.jsp">
					<table>
						<tr>
							<td><INPUT TYPE=TEXT NAME=date> &nbsp;
							<INPUT TYPE=SUBMIT VALUE="Go!" class="mySubmitButton"></td>							
						</tr>
						<tr>
							<td><INPUT TYPE=SUBMIT NAME=month VALUE="I'd like the report for current month" class="myButton"></td>
						</tr>
					</table>
				</form>
			</div>
	
		</div>
	</div>
	<%
		// get main menu page
		String menuPage = (String) session.getAttribute(WebCommons.MAIN_MANU);
	%>
	<div id="footer">
		<span class="myLink"><A HREF="<%="../../menus/" + menuPage%>">Back
				to Menu</A></span>
	</div>

	<%
		// extract controller from session			
		ManagerController controller = (ManagerController) session.getAttribute(WebCommons.CONTROLLER);
	
		// check if submitted date
		if (dateData.isDataSet() && (request.getParameter("month") == null 
				|| request.getParameter("month").equals(""))) {
			
			
			// check if got a day
			if (dateData.isDay()) {
				
				try {
					
					// get sales report of given day
					double salesReport = controller.getSalesReport(dateData.getDate());
			
					// display report
					%>
					<span class="notify"><br>On the <%= dateData.getDate() %>, there were sales in worth of <%= salesReport %></span>
					<%
					
				} catch (DBException e) {
					
					// move to error page
					response.sendRedirect("../../commonPages/ErrorPage.jsp");
				}
				
			} else { // input does not specify a day
				
				// given value is not a valid date
				%>
				<span class="notify"><br>Sorry, but input is of wrong format...
				<br>A day should be in format of yyyy-mm-dd</span>
				<%
			}
			
		} else if (!(request.getParameter("month") == null 
				|| request.getParameter("month").equals(""))) { // asked for current month
			
			try {
				
				// get sales report of current month
				double salesReport = controller.getSalesReport();
				
				// get numeric value of current month				
				int currMonth = Calendar.getInstance().get(Calendar.MONTH);
				
				// get name of current month
				Locale usLocale = new Locale("en_US");
				String month = new DateFormatSymbols(usLocale).getMonths()[currMonth];
				
				// display report
				%>
				<span class="notify"><br>In <%= month %>, there were sales in worth of <%= salesReport %></span>
				<%
			
			} catch (DBException e) {
				
				// move to error page
				response.sendRedirect("../../commonPages/ErrorPage.jsp");
			}
		}
	%>

</body>
</html>